package com.tedu.ds.sessionAndMessage.config;

import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static com.tedu.ds.sessionAndMessage.config.RabbitFanoutConfig.DEAD_QUEUE_PREFIX;
import static com.tedu.ds.sessionAndMessage.config.RabbitFanoutConfig.uuid;

@Configuration
public class RabbitMQConfig {

    private String serverQueueName;
    private String deadQueueName;
    public String getServerQueueName(){
        return QUEUE_NAME+uuid;
    }
    public String getDeadQueueName(){
        return DEAD_QUEUE_PREFIX+uuid;
    }

    public static final String EXCHANGE_NAME = "chat_exchange";  // 交换机名称
    public static final String QUEUE_NAME = "chat_queue-";       // 队列名称
    public static final String ROUTING_KEY = "chat_routing_key-"; // 路由键

    public static final String DEAD_LETTER_EXCHANGE_NAME = "dlx_exchange";  // 死信交换机名称
    public static final String DEAD_LETTER_QUEUE_NAME = "dlx_queue-";        // 死信队列名称
    public static final String DEAD_LETTER_ROUTING_KEY = "dlx_routing_key-"; // 死信路由键

    /**
     * 创建持久化的直接交换机
     * @return DirectExchange 对象
     */
    @Bean
    public DirectExchange chatExchange() {
        return new DirectExchange(EXCHANGE_NAME, true, false);  // 第二个参数为 true 表示持久化
    }

    /**
     * 创建持久化的队列
     * @return Queue 对象
     */
    @Bean
    public Queue chatQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE_NAME);  // 设置死信交换器
        args.put("x-dead-letter-routing-key", DEAD_LETTER_ROUTING_KEY+uuid); // 设置死信路由键

        return new Queue(QUEUE_NAME+uuid, true,false,false,args);  // 第二个参数为 true 表示持久化
    }

    /**
     * 绑定队列到交换机
     * @param chatQueue 队列对象
     * @param chatExchange 交换机对象
     * @return Binding 对象
     */
    @Bean
    public Binding chatBinding(Queue chatQueue, DirectExchange chatExchange) {
        return BindingBuilder.bind(chatQueue).to(chatExchange).with(ROUTING_KEY+uuid);  // 使用指定的路由键绑定
    }

    /**
     * 创建死信交换机
     * @return DirectExchange 对象
     */
    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(DEAD_LETTER_EXCHANGE_NAME, true, false);  // 第二个参数为 true 表示持久化
    }

    /**
     * 创建死信队列
     * @return Queue 对象
     */
    @Bean
    public Queue deadLetterQueue() {
        return new Queue(DEAD_LETTER_QUEUE_NAME+uuid, true);  // 第二个参数为 true 表示持久化
    }

    /**
     * 绑定死信队列到死信交换机
     * @param deadLetterQueue 死信队列对象
     * @param deadLetterExchange 死信交换机对象
     * @return Binding 对象
     */
    @Bean
    public Binding deadLetterBinding(Queue deadLetterQueue, DirectExchange deadLetterExchange) {
        return BindingBuilder.bind(deadLetterQueue).to(deadLetterExchange).with(DEAD_LETTER_ROUTING_KEY+uuid);  // 使用指定的路由键绑定
    }

    /**
     * 创建自定义的 RabbitListenerContainerFactory 用于死信队列
     * @param connectionFactory 连接工厂
     * @return RabbitListenerContainerFactory 对象
     */
    @Bean
    public RabbitListenerContainerFactory<SimpleMessageListenerContainer> deadLetterListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setConcurrentConsumers(2);  // 设置并发消费者数量为 2
        factory.setMaxConcurrentConsumers(2);  // 设置最大并发消费者数量为 2
        factory.setPrefetchCount(10);  // 设置预取计数为 10
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);  // 设置手动确认模式
        return factory;
    }


}